package com.woniu.until;

import cn.hutool.core.util.ObjectUtil;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * ClassName: CookieUtil
 * Description:
 *
 * @author whale
 * @Create 2023/10/7 20:04
 * @Version 1.0
 */
public class CookieUtil {
    /**
     * 获取cookie的值
     *
     * @param name
     * @param request
     * @return
     */
    public static String getCookie(String name, HttpServletRequest request) {
        String value = null;
        Cookie[] cookies = request.getCookies();
        if (ObjectUtil.isNull(cookies)) {
            return null;

        }
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(name)) {
                value = cookie.getValue();
            }
        }

        return value;
    }

    public static void getCookie(String name, String value, HttpServletResponse response) {
        Cookie cookie = new Cookie(name, value);
        cookie.setDomain("localhost");
        cookie.setPath("/");
        response.addCookie(cookie);
    }


}
